package com.dubbo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dubbo.common.ResponseUtil;
import com.dubbo.common.check.CheckUtil;
import com.dubbo.common.page.PageResponse;
import com.dubbo.common.page.PageUtils;
import com.dubbo.domain.Address;
import com.dubbo.domain.info.AddressInfo;
import com.dubbo.domain.info.ResultMessage;
import com.dubbo.mapper.AddressMapper;
import com.dubbo.service.business.address.AddressService;
import com.dubbo.service.impl.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 收货地址
 * 
 * @author Administrator
 * @date 2018年12月17日 下午4:48:47
 */
@Service
@Transactional(readOnly=false,rollbackFor=Exception.class)
public class AddressServiceImp extends BaseServiceImpl<Address> implements AddressService {

	@Resource
	private AddressMapper addressMapper;
	
	/**
	 * 显示收货地址
	 * @author gujiahua
	 * 2018年12月17日 下午5:26:47
	 * @param userId
	 * @param pageStart
	 * @param pageTotal
	 * @return
	 */
	@Override
	public Map<String, Object> displayAddress(String userId,Long pageStart,Long pageTotal) {
		//数据初始化
		pageStart = PageUtils.initialPageStart(pageStart);
		pageTotal = PageUtils.packagePageTotal(pageTotal, 5, 10);
		Long countPageStart = PageUtils.countPageStart(pageStart, pageTotal);
		
		List<Map<String, Object>> addressList = 
				addressMapper.getAddressList(userId, countPageStart, pageTotal);
		Long pageCount = addressMapper.getAddressListCount(userId);
		return ResponseUtil.ajaxCheckSuccess(AddressInfo.OK,new PageResponse(pageStart, pageTotal, pageCount, addressList));
	}

	/**
	 * 新增收货地址
	 * @author gujiahua
	 * 2018年12月17日 下午5:26:53
	 * @param userId
	 * @param addressJson
	 * @return
	 */
	@Override
	public Map<String, Object> addAddress(String userId,String addressJson) {
		Address address=JSONObject.parseObject(addressJson,Address.class);
		address.setUserid(userId);
		//通用校验基础地址数据
		Map<String, Object> checkMap = this.Verification(address);
		if(checkMap!=null) {
			return checkMap;
		}
		//新增时,检查有没有默认地址
		if(address.getDefaultaddress().equals(1)) {
			Address ads=new Address();
			ads.setUserid(address.getUserid());
			ads.setDefaultaddress(1);
			ads=addressMapper.selectOne(ads);
			if(!StringUtils.isEmpty(ads)) {
				ads.setDefaultaddress(0);
				ads.setUpdateTime(new Date());
				addressMapper.updateByPrimaryKeySelective(ads);
			}
		}else {
			Address ads=new Address();
			ads.setUserid(address.getUserid());
			ads.setDefaultaddress(1);
			ads=addressMapper.selectOne(ads);
			if(!StringUtils.isEmpty(ads)) {
				address.setDefaultaddress(0);
			}
		}
		address.setId(UUID.randomUUID().toString().replace("-", ""));
		address.setCreateTime(new Date());
		address.setUpdateTime(new Date());
		addressMapper.insertSelective(address);
		return ResponseUtil.ajaxCheckSuccess(AddressInfo.ADD_SUCCESS);
	}

	/**
	 * 字段验证
	 * @author gujiahua
	 * 2018年12月17日 下午5:27:00
	 * @param address
	 * @return
	 */
	@Override
	public Map<String, Object> Verification(Address address) {
		if(StringUtils.isEmpty(address.getName())) {
			return ResponseUtil.ajaxCheckError(AddressInfo.CONSIGNEE_NOT);
		}
		if(StringUtils.isEmpty(address.getPhone())) {
			return ResponseUtil.ajaxCheckError(AddressInfo.NUMBER_NOT);
		}
		if(StringUtils.isEmpty(address.getProvince())) {
			return ResponseUtil.ajaxCheckError(AddressInfo.PROVINCE_NOT);
		}
		if(!CheckUtil.checkMobile(address.getPhone())){
			return ResponseUtil.ajaxCheckError(AddressInfo.PHONE_ILLEGAL);
		}
//		if(address.getProvince().equals("西藏自治区")||address.getProvince().equals("新疆维吾尔自治区")||address.getProvince().equals("内蒙古自治区")) {
//			return ResponseUtil.ajaxCheckError(AddressInfo.REGION_INVALID);
//		}
		if(StringUtils.isEmpty(address.getCity())){
			return ResponseUtil.ajaxCheckError(AddressInfo.CITY_NOT);
		}
		if(StringUtils.isEmpty(address.getArea())) {
			return ResponseUtil.ajaxCheckError(AddressInfo.AREA_NOT);
		}
		if(StringUtils.isEmpty(address.getAddress())) {
			return ResponseUtil.ajaxCheckError(AddressInfo.ADDRESS_NOT);
		}
		return null;
	}

	/**
	 * 修改收货地址
	 * @author gujiahua
	 * 2018年12月17日 下午5:27:09
	 * @param userId
	 * @param addressJson
	 * @return
	 */
	@Override
	public Map<String, Object> updateAddress(String userId,String addressJson) {
		Address address=JSONObject.parseObject(addressJson,Address.class);
		address.setUserid(userId);
		//通用校验基础地址数据
		Map<String, Object> checkMap = this.Verification(address);
		if(checkMap!=null) {
			return checkMap;
		}
		if(address.getId()==null || !org.apache.commons.lang3.StringUtils.isNotBlank(address.getId())){
			return ResponseUtil.ajaxCheckError(AddressInfo.PARAMETER_NULL);
		}
		//修改时,检查有没有默认地址
		if(address.getDefaultaddress().equals(1)) {
			Address ads=new Address();
			ads.setUserid(address.getUserid());
			ads.setDefaultaddress(1);
			ads=addressMapper.selectOne(ads);
			if(!StringUtils.isEmpty(ads)) {
				if(!ads.getId().equals(address.getId())) {
					ads.setDefaultaddress(0);
					ads.setUpdateTime(new Date());
					addressMapper.updateByPrimaryKeySelective(ads);
				}
			}
		}
		address.setUpdateTime(new Date());
		addressMapper.updateByPrimaryKeySelective(address);
		return ResponseUtil.ajaxCheckSuccess(AddressInfo.UPDATE_SUCCESS);
	}

	/**
	 * 删除收货地址
	 * @author gujiahua
	 * 2018年12月17日 下午5:27:15
	 * @param id
	 * @return
	 */
	@Override
	public Map<String, Object> deleteAddress(String id) {
		if(!StringUtils.isEmpty(id)) {
			Address address = new Address();
			address.setId(id);
			Address ads = addressMapper.selectOne(address);
			//如果是默认地址不能删除
			if(ads==null||ads.getDefaultaddress()==null||ads.getDefaultaddress().equals(1)){
				return ResponseUtil.ajaxCheckError(AddressInfo.DELETE_FAIL);
			}
			addressMapper.deleteByPrimaryKey(id);
			return ResponseUtil.ajaxCheckSuccess(AddressInfo.DELETE_SUCCESS);
		}else {
			return ResponseUtil.ajaxCheckError(AddressInfo.PARAMETER_NULL);
		}
	}

	/**
	 * 修改默认地址
	 * @author gujiahua
	 * 2018年12月17日 下午5:31:40
	 * @param userId
	 * @param addressJson
	 * @return
	 */
	@Override
	public Map<String, Object> updateDefaultaddress(String userId,String addressJson) {
		Address address=JSONObject.parseObject(addressJson,Address.class);
		address.setUserid(userId);
		//数据校验
		if(address.getId()==null || !org.apache.commons.lang3.StringUtils.isNotBlank(address.getId())){
			return ResponseUtil.ajaxCheckError(AddressInfo.PARAMETER_NULL);
		}
		if(address.getDefaultaddress()==null || address.getDefaultaddress()>1 || address.getDefaultaddress()<0){
			return ResponseUtil.ajaxCheckError(AddressInfo.PARAMETER_ERROR);
		}
		if(address.getDefaultaddress().equals(1)) {
			Address ads=new Address();
			ads.setUserid(address.getUserid());
			ads.setDefaultaddress(1);
			ads=addressMapper.selectOne(ads);
			if(!StringUtils.isEmpty(ads)) {
				if(!ads.getId().equals(address.getId())) {
					ads.setDefaultaddress(0);
					ads.setUpdateTime(new Date());
					addressMapper.updateByPrimaryKeySelective(ads);
					addressMapper.updateByPrimaryKeySelective(address);
				}
			}else {
				address.setUpdateTime(new Date());
				addressMapper.updateByPrimaryKeySelective(address);
			}
		}else {
			address.setUpdateTime(new Date());
			addressMapper.updateByPrimaryKeySelective(address);
		}
		return ResponseUtil.ajaxCheckSuccess(AddressInfo.UPDATE_SUCCESS);
	}

	/**
	 * 获取用户的默认地址
	 * @author gujiahua
	 * 2018年12月17日 下午5:27:28
	 * @param userId
	 * @return
	 */
	@Override
	public Map<String, Object> getOneDefaultaddress(String userId) {
		Address address=new Address();
		address.setUserid(userId);
		address.setDefaultaddress(1);
		List<Address> list=addressMapper.select(address);
		if(list!=null && list.size()>0) {
			return ResponseUtil.ajaxCheckSuccess(AddressInfo.OK, list.get(0));
		}else {
			return ResponseUtil.ajaxCheckSuccess(AddressInfo.ADD_ADDRESS);
		}
	}

	/**
	 * 获取修改地址详情
	 * @author wujunyuan
	 * @date 2018年12月28日 下午5:30:12
	 * @param addressId 修改地址原id
	 * @return
	 */
	@Override
	public Map<String, Object> getUpdateDefaultAddress(String addressId) {
		//校验参数
		if(org.apache.commons.lang3.StringUtils.isBlank(addressId)) {
			return ResponseUtil.ajaxCheckError(ResultMessage.PARAMETER_ERROR,"addressId error~");
		}
		//根据addressId获取地址详情
		Map<String,Object> result = addressMapper.getUpdateDefaultAddress(addressId);
		return ResponseUtil.ajaxCheckSuccess(ResultMessage.OK, result);
	}

}
